package com.ss.android.article.base.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;

import com.bytedance.common.utility.reflect.Reflect;

public class SettingsActivityHelper {
    public SettingsActivityHelper() {

    }

    public static int areNotificationsEnabled(Context context) {
        int areNotificationsEnabled = SettingsActivityHelper.queryAreNotificationsEnabled(context);
        if (areNotificationsEnabled == 0) {
            return 0;
        } else if (areNotificationsEnabled == 1) {
            return 1;
        } else {
            return -1;
        }
    }

    private static boolean canHandleIntent(Intent intent, Context context) {
        return context != null && intent != null && context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
    }

    public static void gotoSettingsActivity(Context context) {
        if (context != null
                && !startAppMarket(context)
                && !startAppDetailSettingsActivity(context)) {
            startAppSettingsActivity(context);
        }
    }

    private static int queryAreNotificationsEnabled(Context context) {
        if (context == null) {
            return -1;
        }

        if (Build.VERSION.SDK_INT >= 24) {
            try {
                if (!(Boolean) Reflect.on(context.getSystemService(Context.NOTIFICATION_SERVICE)).call("areNotificationsEnabled").target) {
                    return 0;
                } else {
                    return 1;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

        try {
            Object appOpsService = context.getSystemService(Context.APP_OPS_SERVICE);
            ApplicationInfo applicationInfo = context.getApplicationInfo();
            String packageName = context.getApplicationContext().getPackageName();
            int uid = applicationInfo.uid;
            Reflect reflect = Reflect.on(appOpsService);
            if ((Integer) reflect.call("checkOpNoThrow",
                    new Class[]{Integer.TYPE, Integer.TYPE, String.class},
                    new Object[]{(Integer) reflect.get("OP_POST_NOTIFICATION", new Class[]{Integer.TYPE}).target, Integer.valueOf(uid), packageName}).target != 0) {
                return 0;
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return -1;
        }
        return 1;
    }

    private static boolean startAppMarket(Context context) {
        Intent intent;
        if (context == null) {
            return false;
        } else {
            try {
                intent = new Intent();
                intent.setComponent(new ComponentName("com.coloros.notificationmanager", "com.coloros.notificationmanager.AppDetailPreferenceActivity"));
                intent.putExtra("pkg_name", context.getPackageName());
                intent.putExtra("app_name", context.getResources().getString(context.getApplicationInfo().labelRes));
                intent.putExtra("class_name", SettingsActivityHelper.class.getName());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (canHandleIntent(intent, context)) {
                    context.startActivity(intent);
                    return true;
                }
                intent = new Intent();
                intent.setComponent(new ComponentName("com.oppo.notification.center", "com.oppo.notification.center.AppDetailActivity"));
                intent.putExtra("pkg_name", context.getPackageName());
                intent.putExtra("app_name", context.getResources().getString(context.getApplicationInfo().labelRes));
                intent.putExtra("class_name", SettingsActivityHelper.class.getName());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (canHandleIntent(intent, context)) {
                    context.startActivity(intent);
                    return true;
                }
                return false;
            } catch (Exception exception) {
                return false;
            }
        }
    }

    private static boolean startAppDetailSettingsActivity(Context context) {
        try {
            Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS", Uri.parse("package:" + context.getPackageName()));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (canHandleIntent(intent, context)) {
                context.startActivity(intent);
                return true;
            } else {
                return false;
            }
        } catch (Exception exception) {
        }
        return false;
    }

    private static boolean startAppSettingsActivity(Context context) {
        try {
            Intent intent = new Intent("android.settings.APPLICATION_SETTINGS");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (canHandleIntent(intent, context)) {
                context.startActivity(intent);
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
        }

        return false;
    }
}

